package org.ediba.siri.core.model.data;

import java.util.Collection;
import java.util.Map;

import org.ediba.siri.core.Filter;
import org.ediba.siri.core.common.data.HibernateDomainDao;
import org.ediba.siri.core.model.domain.ModelModel;
import org.ediba.siri.core.model.domain.ModelModelId;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;

public class HibernateModelModelDao extends HibernateDomainDao<ModelModel, ModelModelId> implements ModelModelDao {

	public HibernateModelModelDao() {super(ModelModel.class);}

	@Override
	protected DetachedCriteria buildCriteria(Collection<Map<Filter, Object>> filters) {
		DetachedCriteria detachedCriteria = this.buildCriteria();
		
		Disjunction or = Restrictions.disjunction();
		Conjunction and;

		for (Map<Filter, Object> map : filters) {
			and = Restrictions.conjunction();
			for (Map.Entry<Filter,Object> entry: map.entrySet())
				switch (entry.getKey()) {
					case MODELB_ID:	and.add(Restrictions.eq(Filter.MODELB_ID.getProperty(), (Integer)entry.getValue()));
					break;
				};
			or.add(and);
		}
		detachedCriteria.add(or);
		
		return detachedCriteria;
	}
}
